<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\OperationLog;
use Gate;
class OperationLogController extends Controller
{

    public function operation_log_gate($value='')
    {
        if (!Gate::allows('operationLog','')) {
            throw new PermissionDeniedException();
        }//end if()
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(OperationLog $operationLog,Request $request)
    {
        $this->operation_log_gate();
//        $operationLog = $operationLog->orderBy('time','desc');
//        $user = trim($request->get('user_name'));

        $query =  $operationLog->orderBy('time','desc');

        $filter = [
            'user' => trim($request->get('user_name')),
            'operation_type'=>trim($request->get('operation_type')),
            'operation_date'=>trim($request->get('operation_date')),
            'operation_id'=>trim($request->get('operation_id')),
            'time'=>$request->get('time'),
        ];

        //用户名
        if ($filter['user']&&$filter['user']!=''){
            $query = $query->where('user','like','%'.$filter['user'].'%');
        }

        //操作类型
        if ($filter['operation_type']&&$filter['operation_type']!=''){
            $query = $query->where('operation_type','like','%'.$filter['operation_type'].'%');
        }

        //操作表
        if ($filter['operation_date']&&$filter['operation_date']!=''){
            $query = $query->where('operation_date','like','%'.$filter['operation_date'].'%');
        }

        //操作表
        if ($filter['operation_id']&&$filter['operation_id']!=''){
            //dd($filter['operation_id']);
            $query = $query->where('operation_id','like','%'.$filter['operation_id'].'%');
        }

        //时间
        if ($filter['time']&&$filter['time']!='ALL'){
            $time = explode('|',$filter['time']);

            $query = $query->where('time','>=',$time[0]);
            $query = $query->where('time','<=',$time[1]);
        }

        $operationLogs = $query->paginate(15);
        return view('admin.operationLog.index',compact('operationLogs','filter'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
